#! /bin/sh
# PCP QA Test No. 651
# basic pmproxy functionality
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

[ -x $PCP_BINADM_DIR/pmproxy ] || \
    _notrun "need $PCP_BINADM_DIR/pmproxy"


hostname=`_get_fqdn`
[ "$hostname" = "localhost" -o "$hostname" = "localhost.localdomain" ] && \
    _notrun "need sensible setup, not simply hostname => localhost"

echo "hostname=$hostname" >>$seq_full

# for newer versions, we default to local sockets with different
# access controls.  dodge that, and test the usual pmproxy path.
target="-h $hostname"

_filter()
{
    sed \
	-e "s/$hostname/MYHOST/" \
	-e "s/`hostname`/MYHOST/" \
	-e "s/localhost/MYHOST/" \
	-e "s/local:/MYHOST/" \
	-e '/hinv.ncpu/s/ 1 [0-9][0-9]*/ 1 <n>/'
}

_filter_pmproxy()
{
    sed \
	-e '/^pmproxy: disabled time series, requires libuv support (missing)/d' \
    # end

}

signal=$PCP_BINADM_DIR/pmsignal
username=`id -u -n`
status=0	# success is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

pmproxy_was_running=false
[ -f $PCP_RUN_DIR/pmproxy.pid ] && pmproxy_was_running=true
echo "pmproxy_was_running=$pmproxy_was_running" >>$seq_full

_cleanup()
{
    $sudo $signal -a pmproxy
    _wait_for_stop
    echo "final pmproxy log ..." >>$seq_full
    cat $tmp.log >>$seq_full
    _restore_auto_restart pmproxy
    if $pmproxy_was_running
    then
	echo "Restart pmproxy ..." >>$seq_full
	_service pmproxy restart >>$seq_full 2>&1
	_wait_for_pmproxy
    fi
    $sudo rm -f $tmp.*
}

_wait_for_start()
{
    rm -f $tmp.started
    for i in 1 2 3 4 5 6 7 8 9
    do
	echo "startup delay i=$i" >>$seq_full
	if pmprobe $target -v sample.long.hundred >$tmp.tmp 2>&1
	then
	    cat $tmp.tmp >>$seq_full
	    sts=`$PCP_AWK_PROG <$tmp.tmp '{print $2}'`
	    echo "sts=$sts" >>$seq_full
	    if [ "$sts" = 1 ]
	    then
		touch $tmp.started
		break
	    fi
	else
	    cat $tmp.tmp >>$seq_full
	fi
	sleep 1
    done
    if [ -f $tmp.started ]
    then
	$PCP_PS_PROG $PCP_PS_ALL_FLAGS \
	| grep -E 'PID|pmproxy|pmcd' >>$seq_full
    else
	# NB: may be an expected situation at this stage of the test
	echo "Failed to probe pmproxy" >>$seq_full
	$PCP_PS_PROG $PCP_PS_ALL_FLAGS \
	| grep -E 'PID|pmproxy|pmcd' >>$seq_full
	[ -f $tmp.log ] && cat $tmp.log >>$seq_full
    fi
}

_wait_for_stop()
{
    rm -f $tmp.stopped
    for i in 1 2 3 4 5 6 7 8 9
    do
	echo "shutdown delay i=$i" >>$seq_full
	if pmprobe $target -v sample.long.hundred >>$seq_full 2>&1
	then
	    :
	else
	    touch $tmp.stopped
	    break
	fi
	sleep 1
    done
    if [ -f $tmp.stopped ]
    then
	:
    else
	echo "Failed to stop pmproxy"
	$PCP_PS_PROG $PCP_PS_ALL_FLAGS \
	| grep -E '[P]PID|[p]mproxy|[p]mcd'
    fi
}

# real QA test starts here
export PMCD_HOST=localhost
export PMPROXY_HOST=$hostname
export PMPROXY_PORT=44322
_stop_auto_restart pmproxy

if ! _service pmproxy stop >>$seq_full 2>&1; then _exit 1; fi
_wait_for_stop

proxyargs="-l $tmp.log"
proxyargs="$proxyargs -Dcontext" # -Dcontext,pdu
id pcp >/dev/null 2>&1 && proxyargs="$proxyargs -U pcp"

echo "== pmproxy not running ==" | tee -a $seq_full
pmprobe $target -v sample.long.hundred 2>&1 | _filter

echo
echo "== simplest default case ==" | tee -a $seq_full
$sudo $PCP_BINADM_DIR/pmproxy $proxyargs 2>&1 \
| _filter_pmproxy
_wait_for_start

pmprobe $target -v sample.long.hundred 2>&1 | _filter

echo
echo "== simple explicit host case ==" | tee -a $seq_full
pmprobe -v -h localhost hinv.ncpu 2>&1 | _filter

echo
echo "== non-standard PMPROXY_PORT, expect failure ==" | tee -a $seq_full
export PMPROXY_PORT=94322
pmprobe $target -v sample.long.hundred 2>&1 | _filter

$sudo $PCP_BINADM_DIR/pmsignal -a pmproxy
_wait_for_stop
echo "1st pmproxy log ..." >>$seq_full
cat $tmp.log >>$seq_full

echo
echo "== non-standard PMPROXY_PORT, expect success ==" | tee -a $seq_full
$sudo $PCP_BINADM_DIR/pmproxy $proxyargs 2>&1 \
| _filter_pmproxy
_wait_for_start

pmprobe $target -v sample.long.hundred 2>&1 | _filter

unset PMPROXY_PORT
$sudo $PCP_BINADM_DIR/pmsignal -a pmproxy
_wait_for_stop
echo "2nd pmproxy log ..." >>$seq_full
cat $tmp.log >>$seq_full

echo
echo "== restricted interface for connections, expect failure ==" | tee -a $seq_full
$sudo $PCP_BINADM_DIR/pmproxy -i 127.0.0.1 $proxyargs 2>&1 \
| _filter_pmproxy
_wait_for_start

pmprobe $target -v sample.long.hundred 2>&1 | _filter

echo
echo "== restricted interface for connections, expect success ==" | tee -a $seq_full
export PMPROXY_HOST=localhost
pmprobe $target -v sample.long.hundred 2>&1 | _filter

# success, all done
exit
